MPW Special Chars
Volume Number: 6
Issue Number: 7
Column Tag: MPW Notes
MPW Special Characters
By Mark Andrews, Mountain View, CA
Using Special Characters In the Command Language
[Mark Andrews is the author of 15 computer books, including Atari Roots,
Programming the Commodore 64 in Assembly Language, and Programming the Apple
IIGS in Assembly Language and C. His newest book is Inside MPW, to be published this
fall by Addison-Wesley. This article is an excerpt from Inside MPW.]
There are two ways to go about designing a computer language. You can construct
it like a spoken language, so that it will be easy to learn and understand. Or you can
design it using a more concise but less English-like model, so it will be faster, more
efficient--and, all too often, quite difficult to master.
When the creators of MPW sat down to develop a shell language, they could have
made it a lot more user-friendly. For instance, they could have used the word TOP
instead of the character • to represent the beginning of a file, the word BOTTOM instead
of the symbol ∞ to represent the bottom, and the word SELECTION instead of § to
represent the currently selected, or highlighted, text. That kind of approach would
have made learning the MPW shell language a much less formidable task than it has
turned out to be.
There would have been tradeoffs, of course. Once you’ve mastered the MPW
command language, it’s much faster to type a command like •:∞ than it is to type
something like FROM TOP TO BOTTOM SELECT ALL, which would be a possible
alternative in a more English-like language. And a command interpreter can certainly
parse three ASCII characters much faster than it could handle a long sequence of words
in a more user-friendly language.
But that’s really all quite academic. Like it or not, the MPW shell language is
what we’ve got, and if you want to use MPW, there’s no alternative but to learn the
MPW command language.
Actually, MPW language uses two sets of special characters. One set is made up of
the punctuation marks and other special symbols that are printed on the Macintosh
keyboard. The other set comes from the Macintosh extended character set: the set of
characters that you get when you press a key on your keyboard while holding the
Option key down. And those extended characters can be a real headache when you’re
trying to master MPW. Not only do you have to learn how they’re used in the command
language; since they don’t appear on the keyboard, you also have to figure out--and
then memorize--where to find them.
To make matters even more difficult, many special characters have more than
one meaning in MPW; when they appear in one context, they mean one thing, and when
they’re used in a different context, they often mean another.
The most notorious character with two meanings is undoubtedly the symbol ∂
(Option-D). When the ∂ character appears alone at the end of a line, MPW runs that
line and the next line together, creating a single line. But when ∂ precedes the letter n,
it acts as an escape character and generates a return.
Think about this for a moment, and you’ll realize that the character ∂ has two
meanings that are exact opposites. Sometimes it deletes a return, and sometimes it
creates one!
There are many other special characters that have more than one meaning in
MPW. For example, an exclamation point means “not” in string and arithmetic
operations, but stands for a line of text in certain editing operations. The character §
sometimes stands for the current selection (either a block of highlighted text or the
current position of the cursor), and sometimes stands for the name of a file. And so on.
One way to sort out the ambiguities in the special characters used by MPW would
be to to list every meaning of every special character used in the MPW command
language, and then to examine and resolve each ambiguity that you find. And that’s just
what has been done in Table 1 (except those used as arithmetic and logical
operators/in menu commands/as number prefixes in arithmetic and logical
operations), which appears at the end of this article. In addition to listing the
meanings and categories of all special characters used in the MPW language, it shows
how to type each character, describes the syntax in which each character is used, and
provides an example showing how each character can be used in a command.
In compiling Table 1, I discovered that the MPW command language character set
contains fourteen distinct kinds of characters. Since no breakdown of this kind had
been published until I started working on Inside MPW, I took the liberty to draw up my
own list of categories. From this re search, these were the categories that emerged:
• Whitespace characters
• Command terminators
• Wildcard characters
• The escape character ∂
• The line-continuation character ∂
• The comment character #
• Delimiters
• The command substitution character ‘
• Filename generation operators
• Selection expressions
• Regular expression operators
• Redirection operators
• The number prefixes $, 0x, 0b, and 0 (not in Table 1)
• Arithmetical and logical operators (not in Table 1)
• Special characters used in make files
Whitespaces and Command Terminators
In MPW, a command is defined as a series of words and regular expressions
separated by whitespaces and ending with a command terminator. There are only two
whitespace characters in the MPW command language: Space and Tab. You can use
either character to generate a space in an MPW command.
The most commonly used command terminator is the Return. Unless a return is
followed immediately by the line continuation character ∂, it always ends a command.
Another command terminator that you’ll often see is the semicolon (;). By using a
semicolon as a command terminator, you can type more than one command on a line.
The special-character combinations && and || are logical operators as well as
command terminators. If you separate two commands with the characters &&, the
second command will be executed only if the first command succeeds. Conversely, if
you separate two commands with the characters ||, the second command will be
executed only if the first command fails.
Selection Expressions
Selection expressions used in MPW include • (Option 8), which represents the
beginning of a file; ∞ (Option-5), which represents the end of a file; and §
(Option-6) which represents the current selection. Another selection expression is
the character (Option-J), which can be used to represent either the beginning or the
end of a selection.
Delimiters
Many kinds of delimiters are used in the MPW command language. When you
want to include a space or a special character in a string or an expression, you must
enclose the command in single or double quotation marks.
If you use single quotes around an expression, every special character in the
expression is interpreted literally, instead of being interpreted as a special character.
If you use double quotes, all characters in the expression are taken literally except
curly brackets ({...}), the backquote character (‘), and the escape character ∂. If you
want to use a shell variable as part of an expression, you must enclose the expression
in single quotes, since variables in MPW are always delimited by curly brackets. And
if the definition of a variable contains white spaces, then the curly brackets that
enclose the variable must themselves be enclosed in quotes, like this: “{MPW}”.
If you want to use an apostrophe in a string, and don’t want it be be interpreted as
a single quote, you can put double quotes around the word containing the apostrophe. Or
you can precede the apostrophe with the escape character ∂.
The slash bar (/) and the backslash (\) are often used as delimiters with the
commands, Find, Search, and Replace. When a string or expression delimited by
slash bars (/.../) follows a Find command, Find searches in a forward direction. But
when Find is followed by a string or expression enclosed in backslashes (\...\), the
search goes in a backwards direction. Hence, to start at the beginning of a file and
search for the beginning of the string “charlie,” you could execute the commands
Find • ; Find /charlie/
But if you wanted to start at the end of a file and search backwards for end of the
string “charlie,” you could execute the commands
Find ∞ ; Find \charlie
One very interesting delimiter is the backquote character (‘). By placing a
command between a pair backquote characters, you can pass its output to another
command. For example, when you execute the command
Echo ‘Files -t TEXT‘
the Files command compiles a list of files of the type TEXT, and passes the list to
the Echo command, which then prints the list on the screen.
If there were enough space in this magazine, I could examine every special
character used in MPW, and present interesting examples for each one--and, in Inside
MPW, that’s exactly what is done. Meanwhile, until the book comes out, take some
time to study the examples in Table 1. Unless you’re already an MPW wizard, chances
are they can tell you a lot about how to use special characters in MPW.
AppleLink: D3001
TABLE 1: Special Characters Used in MPW
Chr Press Category Usage Meaning Example Translation
# # Comment #s Characters between # A comment String following # is interpreted as a comment
# and terminator
are interpreted
as a comment
" " Delimiter "s" Delimits a string Echo "{MPW}" >> Echo the contents of the shell variable {MPW}
in which each "{Target}" to the target window
character is taken
literally, except
for ∂, {}, and ‘
' ' Delimiter 's' Delimits a string Echo '{MPW}' >> Echo the string "{MPW}" to the target window
in which all "{Target}
characters are
taken literally
( ( Delimiter (p) Delimits a group Find /("*")+/ Select a group of one or more asterisks
of characters that
form a pattern
) ) Delimiter (p) Delimits a group Find /("*")+/ Select a group of one or more asterisks
of characters that
form a pattern
/ / Delimiter /r/ Searches forward Find /delta/ Search forward and select the word "delta
and selects regular
expression
äOption-Shift-
Delimiter nëDelimits number Find /[∂t]2/ Select exactly two tabs